package com.shuqi.model;

import java.util.List;

import org.apache.http.message.BasicNameValuePair;

import android.content.Context;

import com.shuqi.application.ShuqiApplication;
import com.shuqi.common.MyTask;
import com.shuqi.common.Urls;
import com.shuqi.common.utils.HttpCommonParamsUtils;
import com.shuqi.controller.MyFavoritController.ActionEnum;
import com.shuqi.database.model.UserInfo;
import com.shuqi.model.manager.MyAccountManager;
import com.shuqi.model.net.HttpRequest;
import com.shuqi.model.net.UrlManager;
import com.shuqi.model.net.listener.HttpRequestListener;
import com.shuqi.model.parse.parser.MyFavoritAddParser;
import com.shuqi.model.parse.parser.MyFavoritListParser;
import com.shuqi.model.parse.parser.MyFavoritRemoveParser;

/**
 * 我的收藏model
 * 
 * @author li.li
 * @date 2014-06-11
 * 
 */
public class MyFavoritModel extends ModelBase implements HttpRequestListener {
    private HttpRequest httpRequest;
    private Context context;
    private ActionEnum actionEnum;
    private String bookId;

    public MyFavoritModel(Context context) {
        this.context = context;
    }

    @Override
    public void action(int actionCode, Object object) {
        fireEvent(actionCode, object);

    }

    @Override
    public List<BasicNameValuePair> setParams() {
        UserInfo info = MyAccountManager.getInstance(ShuqiApplication.getContext());
        return HttpCommonParamsUtils.getShuQiBasicParams(info.getUserId());
    }

    @Override
    public String getRequestUrl() {
        
        if (ActionEnum.getList.equals(actionEnum)) {// 收藏列表
            String userId = MyAccountManager.getInstance(ShuqiApplication.getContext()).getUserId();
            return Urls.getMyFavoritUrl(userId, actionEnum);
        }else if (ActionEnum.remove.equals(actionEnum)) {//删除收藏
            String userId = MyAccountManager.getInstance(ShuqiApplication.getContext()).getUserId();
            return Urls.getMyFavoritUrl(userId,bookId, actionEnum);
        }else{//添加收藏
            String userId = MyAccountManager.getInstance(ShuqiApplication.getContext()).getUserId();
            return Urls.getMyFavoritUrl(userId,bookId, actionEnum);
        }

        
    }

    @Override
    public void loadData(Object... objs) {
      //解析参数
        for(Object obj : objs){
            if (obj instanceof ActionEnum) {//操作类型
                actionEnum = (ActionEnum) obj;
            }else if(obj instanceof String){//书ID
                bookId = (String)obj;
            }
                
        }
        
        //操作业务操作
        for (Object obj : objs) {
            if (obj instanceof ActionEnum) {//操作类型
                actionEnum = (ActionEnum) obj;

                if (ActionEnum.getList.equals(actionEnum)) {// 收藏列表

                    this.httpRequest =
                            new HttpRequest(context, UrlManager.COLUMN_DOMAIN_WRITE,
                                    getRequestUrl(), setParams(), this);
                    this.httpRequest.setDataParseAdapter(new MyFavoritListParser());
                    MyTask.runInBackground(httpRequest, true);

                } else if (ActionEnum.remove.equals(actionEnum)) {//删除收藏
                    this.httpRequest =
                            new HttpRequest(context, UrlManager.COLUMN_DOMAIN_WRITE,
                                    getRequestUrl(), setParams(), this);
                    this.httpRequest.setDataParseAdapter(new MyFavoritRemoveParser());
                    MyTask.runInBackground(httpRequest, true);

                } else if (ActionEnum.add.equals(actionEnum)) {//添加收藏

                    this.httpRequest =
                            new HttpRequest(context, UrlManager.COLUMN_DOMAIN_WRITE,
                                    getRequestUrl(), setParams(), this);
                    this.httpRequest.setDataParseAdapter(new MyFavoritAddParser());
                    MyTask.runInBackground(httpRequest, true);

                }


                break;
            }
        }

    }

    @Override
    public void onDestroy() {
        if (httpRequest != null) {
            httpRequest.abort();
        }

    }

}
